<?php
function pelapor_select($key=false){
	$sql = "select * from pelapor ";
	if ($key){
		$sql .= " where 
				LAPOR_ID like '%$key%' OR
				LAPOR_NAMA like '%$key%' OR
				NIK like '%$key%' ";
	}
	$result = mysql_query($sql);
	return !$result? mysql_error() : $result;
}

function pelapor_load($id=false, $nik=false, $nama=false){
	$sql = "select * from pelapor ";
	if ($id || $nik || $nama) $sql .= " WHERE ";
	if ($id) $sql .= " LAPOR_ID = '$id'";
	if ($nik) $sql .= ($id? " AND ":"" ) . "NIK = '$nik' ";
	if ($nama) $sql .= ($id||$nik? " AND ":"" ) . " LAPOR_NAMA = '$nama'";
	return mysql_query($sql);
}

function pelapor_loadAlamat($alamatID=false, $lapor_id=false){
	$sql = "select * from alamat_pelapor ";
	$sql .= $alamatID||$lapor_id? " WHERE " : "";
	if($alamatID) $sql .= " ID_ALAMAT='$alamatID' ";
	if ($lapor_id) $sql .= ($alamatID? " AND " :"") . " LAPOR_ID='$lapor_id' ";
	return mysql_query($sql);
}

function pelapor_loadTlp($tlp=false, $lapor_id=false){
	$sql = "select * from telpon_pelapor ";
	$sql .= $tlp||$lapor_id? " WHERE " : "";
	if($tlp) $sql .= " NO_TELPON='$tlp' ";
	if ($lapor_id) $sql .= ($tlp? " AND " :"") . " LAPOR_ID='$lapor_id' ";
	return mysql_query($sql);
}

function pelapor_isExistID($id){
	$sql = "select * from pelapor
			where LAPOR_ID = '$id'";
	
	$result = mysql_query($sql);
	$result = mysql_fetch_assoc($result);
	if ($res['LAPOR_ID'] === $id) return true;
	return false;
}

function pelapor_add($LAPOR_ID, $NIK, $LAPOR_NAMA){
	$sql = "insert into pelapor ".
			"values ('$LAPOR_ID', '$NIK', '$LAPOR_NAMA')";
	return mysql_query($sql);
}

function pelapor_addAlamat($ID_ALAMAT, $LAPOR_ID, $NAMA_ALAMAT, $KODE_POS, $KODE_AREA, $KOTA, $PROPINSI){
	$sql = "INSERT INTO alamat_pelapor 
			VALUES ('$ID_ALAMAT', '$LAPOR_ID', '$NAMA_ALAMAT', '$KODE_POS', '$KODE_AREA', '$KOTA', '$PROPINSI')";
	return mysql_query($sql);
}

function pelapor_addTlp($NO_TELPON, $LAPOR_ID){
	$sql = "INSERT INTO telpon_pelapor 
			VALUES ('$NO_TELPON', '$LAPOR_ID')";
	return mysql_query($sql);
}

function pelapor_update($LAPOR_ID, $NIK, $LAPOR_NAMA){
	$sql = "update pelapor ".
			"set LAPOR_NAMA = '$LAPOR_NAMA', 
				NIK = '$NIK'
			where ID_pelapor='$LAPOR_ID'";
	return mysql_query($sql);
}

function pelapor_updateAlamat($ID_ALAMAT, $LAPOR_ID, $NAMA_ALAMAT, $KODE_POS, $KODE_AREA, $KOTA, $PROPINSI){
	$sql = "update pelapor ".
			"set LAPOR_ID = '$LAPOR_ID', 
				NAMA_ALAMAT = '$NAMA_ALAMAT', 
				KODE_POS = '$KODE_POS', 
				KODE_AREA = '$KODE_AREA', 
				KOTA = '$KOTA', 
				PROPINSI = '$PROPINSI'
			where ID_ALAMAT='$ID_ALAMAT'";
	return mysql_query($sql);
}

function pelapor_delete($id){
	$sql = "delete from pelapor where LAPOR_ID='$id'";
	return mysql_query($sql);
}

function pelapor_deleteAlamat($id){
	$sql = "delete from pelapor where ID_ALAMAT='$id'";
	return mysql_query($sql);
}

function pelapor_deleteTlp($id){
	$sql = "delete from telpon_pelapor where NO_TELPON='$id'";
	return mysql_query($sql);
}